<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const userAvatar = ref('https://picsum.photos/id/64/200/200')
const userInfo = ref({
  nickname: '用户123456',
  email: 'user@example.com',
  registrationDate: '2023-06-15',
  lastLoginDate: '2023-11-10',
  postCount: 0,
  favoriteCount: 12,
  viewCount: 234
})

// 检查用户登录状态
const checkLoginStatus = () => {
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  if (!isLoggedIn) {
    router.push('/login')
    return
  }
  username.value = localStorage.getItem('username') || ''
}

// 退出登录函数
const handleLogout = () => {
  localStorage.removeItem('isLoggedIn')
  localStorage.removeItem('username')
  router.push('/login')
}

onMounted(() => {
  checkLoginStatus()
})
</script>

<template>
  <div class="user-profile-container">
    <div class="container">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1>个人中心</h1>
        <p class="page-description">管理您的个人信息和账户设置</p>
      </div>
      
      <!-- 个人信息卡片 -->
      <div class="profile-card">
        <div class="profile-header">
          <div class="avatar-section">
            <img :src="userAvatar" alt="用户头像" class="user-avatar-large">
            <button class="avatar-upload-btn">更换头像</button>
          </div>
          <div class="user-info-section">
            <h2 class="user-nickname">{{ userInfo.nickname }}</h2>
            <p class="username">{{ username }}</p>
            <div class="user-stats">
              <div class="stat-item">
                <span class="stat-number">{{ userInfo.postCount }}</span>
                <span class="stat-label">帖子</span>
              </div>
              <div class="stat-item">
                <span class="stat-number">{{ userInfo.favoriteCount }}</span>
                <span class="stat-label">收藏</span>
              </div>
              <div class="stat-item">
                <span class="stat-number">{{ userInfo.viewCount }}</span>
                <span class="stat-label">浏览</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 账户信息 -->
        <div class="account-info">
          <h3>账户信息</h3>
          <div class="info-grid">
            <div class="info-item">
              <label>注册时间</label>
              <span>{{ userInfo.registrationDate }}</span>
            </div>
            <div class="info-item">
              <label>上次登录</label>
              <span>{{ userInfo.lastLoginDate }}</span>
            </div>
            <div class="info-item">
              <label>邮箱</label>
              <span>{{ userInfo.email }}</span>
            </div>
          </div>
        </div>
        
        <!-- 操作按钮 -->
        <div class="action-buttons">
          <button class="btn-primary">编辑资料</button>
          <button class="btn-danger" @click="handleLogout">退出登录</button>
        </div>
      </div>
      
      <!-- 快捷入口 -->
      <div class="quick-links">
        <h3>快捷入口</h3>
        <div class="links-grid">
          <router-link to="/favorites" class="link-card">
            <div class="link-icon">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M16 4H8C6.89543 4 6 4.89543 6 6V20C6 21.1046 6.89543 22 8 22H16C17.1046 22 18 21.1046 18 20V6C18 4.89543 17.1046 4 16 4Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M14 2H21V9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M14 2V5C14 6.65685 12.6569 8 11 8H2" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </div>
            <h4>我的收藏</h4>
            <p>查看和管理您收藏的内容</p>
          </router-link>
          <router-link to="/history" class="link-card">
            <div class="link-icon">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M19 21L12 16L5 21V5C5 3.89543 5.89543 3 7 3H17C18.1046 3 19 3.89543 19 5V21Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </div>
            <h4>浏览历史</h4>
            <p>查看您的浏览记录</p>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.user-profile-container {
  min-height: 100vh;
  background-color: var(--background-color);
  padding: 24px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
}

.page-header {
  margin-bottom: 32px;
  text-align: center;
}

.page-header h1 {
  font-size: 32px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.page-description {
  font-size: 16px;
  color: var(--text-secondary);
}

.profile-card {
  background-color: var(--card-background);
  border-radius: 12px;
  box-shadow: var(--shadow-md);
  padding: 32px;
  margin-bottom: 32px;
}

.profile-header {
  display: flex;
  align-items: flex-start;
  gap: 24px;
  margin-bottom: 32px;
  flex-wrap: wrap;
}

.avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.user-avatar-large {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid var(--primary-color);
}

.avatar-upload-btn {
  background-color: var(--primary-color);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color var(--transition-fast);
}

.avatar-upload-btn:hover {
  background-color: var(--primary-hover);
}

.user-info-section {
  flex: 1;
  min-width: 300px;
}

.user-nickname {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.username {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.user-stats {
  display: flex;
  gap: 32px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-number {
  font-size: 24px;
  font-weight: 700;
  color: var(--primary-color);
}

.stat-label {
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.account-info {
  margin-bottom: 24px;
}

.account-info h3 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 16px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.info-item label {
  font-size: 14px;
  color: var(--text-muted);
}

.info-item span {
  font-size: 16px;
  color: var(--text-primary);
  font-weight: 500;
}

.action-buttons {
  display: flex;
  gap: 16px;
  justify-content: center;
  flex-wrap: wrap;
}

.btn-primary {
  background-color: var(--primary-color);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  transition: background-color var(--transition-fast);
}

.btn-primary:hover {
  background-color: var(--primary-hover);
}

.btn-danger {
  background-color: var(--error-color);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  transition: background-color var(--transition-fast);
}

.btn-danger:hover {
  background-color: #dc2626;
}

.quick-links {
  background-color: var(--card-background);
  border-radius: 12px;
  box-shadow: var(--shadow-md);
  padding: 32px;
}

.quick-links h3 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 24px;
}

.links-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.link-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  transition: all var(--transition-fast);
  text-decoration: none;
}

.link-card:hover {
  border-color: var(--primary-color);
  background-color: rgba(59, 130, 246, 0.05);
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
}

.link-icon {
  width: 48px;
  height: 48px;
  background-color: var(--primary-light);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--primary-color);
}

.link-card h4 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.link-card p {
  font-size: 14px;
  color: var(--text-secondary);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  
  .user-info-section {
    min-width: auto;
  }
  
  .user-stats {
    justify-content: center;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .links-grid {
    grid-template-columns: 1fr;
  }
  
  .profile-card,
  .quick-links {
    padding: 24px;
  }
}
</style>

<style>
:root {
  --transition-fast: 0.2s ease;
  --primary-light: rgba(59, 130, 246, 0.1);
}
</style>